package vma.nicici.chat.api.map;


import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import vma.nicici.chat.communication.AsynchCallback;
import vma.nicici.chat.communication.GetHttpRequest;
import android.graphics.Bitmap;

public class MapApi  {
	
	public static final String URL = "http://maps.googleapis.com/maps/api/staticmap?";
	
	Point center;
	int	zoom;
	int sizeX;
	int sizeY;
	String maptype="mobile";
	String sensor="false";
	final String key="AIzaSyAS1HeSmaR9Xv21fskhU8goeRVXhv6VlAA";
	final ArrayList<Marker> markers = new ArrayList<Marker>();

	public MapApi(int zoom, int sizeX, int sizeY) {
		this.zoom = zoom;
		this.sizeX = sizeX;
		this.sizeY = sizeY;
	}
	
	public void addMarker(Marker m) {
		markers.add(m);
	}
	
	public void setCenter(Point center) {
		this.center= center;
	}
	
	
	public Bitmap getMap() {
		List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
		
		parameters.add( new BasicNameValuePair("center", center.asUrl()) );
		parameters.add(new BasicNameValuePair("zoom", zoom+""));
		parameters.add(new BasicNameValuePair("size", sizeX+"x"+sizeY));
		parameters.add(new BasicNameValuePair("maptype", maptype));
		parameters.add(new BasicNameValuePair("sensor", sensor));
		
		for (Marker m : markers) {
			parameters.add(new BasicNameValuePair("markers", m.asUrl()));
		}
		
		GetHttpRequest request = new GetHttpRequest(URL, parameters);
		
		return request.getBitmapResponse();
	}
	
	public void getMapAsynch(final AsynchCallback<Bitmap> callback) {
		Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
				    Bitmap r = getMap();
					callback.onFinished(r);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		t.start();
	}
	

	
	

	
	
}
